package com.keke.service.provider;

import com.keke.ado.baseDao;
import com.keke.ado.bill.billDao;
import com.keke.ado.bill.billDaoImpl;
import com.keke.ado.provider.providerDao;
import com.keke.ado.provider.providerDaoImpl;
import com.keke.pojo.Provider;
import com.sun.org.apache.bcel.internal.generic.FADD;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{

    private providerDao providerDao;
    private billDao billDao;

    public ProviderServiceImpl() {
        this.providerDao = new providerDaoImpl();
        this.billDao = new billDaoImpl();
    }

    public boolean add(Provider provider) {
        boolean flag =false;
        Connection connection = null;
        try {
            connection= baseDao.getConnection();
            connection.setAutoCommit(false);
            if(providerDao.add(connection,provider)>0){
                flag=true;
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("==========rollback===========");
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    public List<Provider> getProviderList(String proName, String proCode,int currentPageNo, int pageSize) {
        Connection connection =null;
        List<Provider> providerList=null;
        System.out.println("proName ----> "+proName);
        System.out.println("proCode ----> "+proCode);
        System.out.println("currentPageNo ---> "+currentPageNo);
        System.out.println("pageSize ---> "+pageSize);
        try {
            connection=baseDao.getConnection();
            providerList=providerDao.getProviderList(connection,proName,proCode,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return providerList;
    }

    public int deleteProviderById(String delId) {
        Connection connection = null;
        int billCount = -1;
        try {
            connection=baseDao.getConnection();
            connection.setAutoCommit(false);
            billCount=billDao.getBillCountByProviderId(connection,delId);
            if(billCount==0){
                providerDao.deleteProviderById(connection,delId);
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("============rollback===============");
            billCount=-1;
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(connection,null,null);
        }
        return billCount;
    }

    public Provider getProviderById(String id) {
        Connection connection = null;
        Provider provider = null;
        try {
            connection=baseDao.getConnection();
            provider = providerDao.getProviderById(connection,id);
        } catch (Exception e) {
            e.printStackTrace();
            provider=null;
        }finally {
            baseDao.close(connection,null,null);
        }
        return provider;
    }

    public boolean modify(Provider provider) {
        boolean flag= false;
        Connection connection = null;
        connection = baseDao.getConnection();
        try {
            if(providerDao.modify(connection,provider)>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    //查询记录数
    public int getProviderCount(String proCode, String proName) {
        Connection connection = null;
        int count=0;
        try {
            connection=baseDao.getConnection();
            count = providerDao.getProviderCount(connection, proCode, proName);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return count;
    }
}
